#include "choosefriendwidget.h"
#include <QHBoxLayout>
#include <QScrollArea>
#include <QScrollBar>
#include <QPushButton>
#include <QLabel>
#include <QPainter>
#include "debug.h"

selectFriendItem::selectFriendItem(chooseFriendWidget *owner, const QString &userId, const QIcon &icon, const QString &name, bool isSelected)
    :owner(owner), userId(userId), isEnter(false)
{
    //设置基本属性
    setFixedHeight(70);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    //添加布局管理器
    QHBoxLayout* layout = new QHBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(5, 0, 0, 0);
    setLayout(layout);

    //创建checkBox
    checkBox = new QCheckBox();
    checkBox->setChecked(isSelected);
    checkBox->setFixedSize(35, 35);
    QString style = "QCheckBox { background-color: transparent; } QCheckBox::indicator { width: 25px; height: 25px; image: url(:/resource/images/unchecked.png);}";
    style += "QCheckBox::indicator:checked { image: url(:/resource/images/checked.png);}";
    checkBox->setStyleSheet(style);

    //创建头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(45, 45);
    avatarBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    avatarBtn->setIconSize(QSize(50, 50));
    avatarBtn->setIcon(icon);
    avatarBtn->setStyleSheet("QPushButton {background-color: transparent}");

    //显示名字
    nameLabel = new QLabel();
    nameLabel->setFont(QFont("宋体", 13));
    nameLabel->setStyleSheet("QLabel {background-color: transparent; padding-left: 10px}");
    nameLabel->setText(name);

    //添加到layout中

    layout->addWidget(checkBox);
    layout->addWidget(avatarBtn);
    layout->addWidget(nameLabel);

    //连接信号槽
    connect(checkBox, &QCheckBox::toggled, this, [=](bool checked){
        if(checked)
        {
            owner->addSelectedFriend(userId, icon, name);
        }
        else
        {
            owner->deleteSelectedFriend(userId);
        }
    });

}

void selectFriendItem::paintEvent(QPaintEvent *event)
{
    (void)event;
    QPainter painter(this); //在哪里画
    if(isEnter)
    {
        painter.fillRect(this->rect(), QColor(220, 220, 220));
    }
    else
    {
        painter.fillRect(this->rect(), QColor(243, 243, 243));
    }
}

void selectFriendItem::enterEvent(QEnterEvent *event)
{
    (void)event;
    isEnter = true;
    //用qt内置的机制调用paintEvent刷新
    this->update();
}

void selectFriendItem::leaveEvent(QEvent *event)
{
    (void)event;
    isEnter = false;
    this->update();
}

const QString selectFriendItem::getUserId() const
{
    return userId;
}

void selectFriendItem::setStatus(bool status)
{
    checkBox->setChecked(status);
}

chooseFriendWidget::chooseFriendWidget(QWidget* parent)
    :QDialog(parent)
{
    //设置基本属性
    setWindowTitle("选择添加好友");
    setWindowIcon(QIcon(":/resource/images/logo.png"));
    setFixedSize (700, 550);
    setAttribute(Qt::WA_DeleteOnClose);

    //添加布局管理器
    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(hlayout);

    //初始化
    initLeft(hlayout);
    initRight(hlayout);
}

void chooseFriendWidget::initLeft(QHBoxLayout *layout)
{
    //创建QScrollArea
    QScrollArea* scroll = new QScrollArea();
    scroll->setWidgetResizable(true);
    scroll->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    scroll->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0px}");
    scroll->verticalScrollBar()->setStyleSheet(
        "QScrollBar:vertical {"
        "    border: none;"
        "    background: #f1f1f1;"  // 滚动条轨道颜色
        "    width: 6px;"         // 稍微宽一点更易点击
        "    margin: 0px 0px 0px 0px;"
        "}"
        "QScrollBar::handle:vertical {"
        "    background: #c1c1c1;" // 滚动条手柄颜色
        "    min-height: 20px;"    // 最小高度
        "    border-radius: 3px;"  // 圆角
        "}"
        "QScrollBar::handle:vertical:hover {"
        "    background: #a8a8a8;" // 悬停颜色
        "}"
        "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
        "    height: 0px;"         // 隐藏上下箭头
        "    background: none;"
        "}"
        "QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
        "    background: none;"
        "}"
        );
    scroll->setStyleSheet("QScrollArea {border: none;}");
    layout->addWidget(scroll, 1);

    //创建QScrollArea中的widget
    friendWidget = new QWidget();
    friendWidget->setStyleSheet("QWidget {background-color: transparent};");
    scroll->setWidget(friendWidget);

    //创建widget中的布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(5);
    vlayout->setContentsMargins(20, 10, 20, 0);
    vlayout->setAlignment(Qt::AlignTop);
    friendWidget->setLayout(vlayout);

    //添加item

#if TEST_UI
    for(int i = 0; i < 30; i++)
    {
        addFriend(QString::number(1000 + i), QIcon(":/resource/images/avatar2.png"), "kiki");
    }
#endif

}

void chooseFriendWidget::initRight(QHBoxLayout *layout)
{
    //网格布局
    QGridLayout* glayout = new QGridLayout();
    glayout->setSpacing(10);
    glayout->setContentsMargins(20 , 5 , 20, 20);
    this->setLayout(glayout);
    layout->addLayout(glayout, 1);

    //设置tip
    QLabel* tip = new QLabel();
    tip->setFixedHeight(50);
    tip->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    tip->setFont(QFont("微软雅黑", 16));
    tip->setText("选择联系人");
    tip->setAlignment(Qt::AlignLeft | Qt::AlignBottom);

    //创建滑动区域
    QScrollArea* scroll = new QScrollArea();
    scroll->setWidgetResizable(true);
    scroll->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    scroll->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0px}");
    scroll->verticalScrollBar()->setStyleSheet(
        "QScrollBar:vertical {"
        "    border: none;"
        "    background: #f1f1f1;"  // 滚动条轨道颜色
        "    width: 6px;"         // 稍微宽一点更易点击
        "    margin: 0px 0px 0px 0px;"
        "}"
        "QScrollBar::handle:vertical {"
        "    background: #c1c1c1;" // 滚动条手柄颜色
        "    min-height: 20px;"    // 最小高度
        "    border-radius: 3px;"  // 圆角
        "}"
        "QScrollBar::handle:vertical:hover {"
        "    background: #a8a8a8;" // 悬停颜色
        "}"
        "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
        "    height: 0px;"         // 隐藏上下箭头
        "    background: none;"
        "}"
        "QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
        "    background: none;"
        "}"
        );
    scroll->setStyleSheet("QScrollArea {border: none;}");
    layout->addWidget(scroll, 1);

    //创建QScroll中的widget
    selectedFriendWidget = new QWidget();
    selectedFriendWidget->setStyleSheet("QWidget {background-color: transparent}");
    scroll->setWidget(selectedFriendWidget);

    //给widget中添加布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->setAlignment(Qt::AlignTop);
    selectedFriendWidget->setLayout(vlayout);




    //创建确定按钮
    QString style = "QPushButton {background-color: rgb(230, 230, 230); border-radius: 10px; color: rgb(9, 200, 90)}";
    style += "QPushButton:pressed {background-color: rgb(210, 210, 210)}";

    QPushButton*  okBtn = new QPushButton();
    okBtn->setFixedHeight(50);
    okBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    okBtn->setText("确认");
    okBtn->setStyleSheet(style);

    //创建取消按钮

    QPushButton*  cancelBtn = new QPushButton();
    cancelBtn->setFixedHeight(50);
    cancelBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    cancelBtn->setText("取消");
    cancelBtn->setStyleSheet(style);


    //添加到布局管理其中 一行9列

    glayout->addWidget(tip, 0, 0, 1, 9);
    glayout->addWidget(scroll, 1, 0, 1, 9);
    glayout->addWidget(okBtn, 2, 1, 1, 3);
    glayout->addWidget(cancelBtn, 2, 5, 1, 3);
#if 0
    for(int i = 0; i < 10; i++)
    {
        addSelectedFriend(QString::number(1000 + i), QIcon(":/resource/images/avatar2.png"), "kiki");
    }
#endif
}

void chooseFriendWidget::addFriend(const QString& userId, const QIcon& icon, const QString& name)
{
    selectFriendItem* item = new selectFriendItem(this, userId, icon, name, false);
    friendWidget->layout()->addWidget(item);
}

void chooseFriendWidget::addSelectedFriend(const QString& userId, const QIcon& icon, const QString& name)
{
    selectFriendItem* item = new selectFriendItem(this, userId, icon, name, true);
    selectedFriendWidget->layout()->addWidget(item);
}

void chooseFriendWidget::deleteSelectedFriend(const QString &userId)
{
    //删除右侧的item
    QVBoxLayout* hlayout = dynamic_cast<QVBoxLayout*>(selectedFriendWidget->layout());
    for (int i = hlayout->count() - 1; i >= 0; --i)
    {
        auto item = hlayout->itemAt(i);
        if(item == nullptr || item->widget() == nullptr) continue;
        selectFriendItem* selectItem  = dynamic_cast<selectFriendItem* >(item->widget());
        if(userId != selectItem->getUserId()) continue;
        //延后删除， 用qt的机制删除, 不然会程序奔溃
        hlayout->removeWidget(selectItem);
        selectItem->deleteLater();
    }

    //清除左边的选中状态
    hlayout = dynamic_cast<QVBoxLayout*>(friendWidget->layout());
    for(int i = 0; i < hlayout->count(); i++)
    {
        auto item = hlayout->itemAt(i);
        if(item == nullptr || item->widget() == nullptr) continue;
        selectFriendItem* selectItem  = dynamic_cast<selectFriendItem* >(item->widget());
        if(userId != selectItem->getUserId()) continue;
        selectItem->setStatus(false);
    }
}


